<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Services;

use App\Room\Room;

class RoomManager
{
    public static $ins;

    protected $rooms = [];

    private function __construct()
    {
    }

    private function __clone()
    {
    }

    public static function getIns()
    {
        if (! self::$ins instanceof self) {
            self::$ins = new self();
        }
        return self::$ins;
    }

    public function getRooms()
    {
        return $this->rooms;
    }

    public function setNewRoom($roomId)
    {
        if ($this->checkRoomExist($roomId)) {
            return;
        }
        $this->rooms[$roomId] = new Room($roomId, 6, 1);
    }

    public function joinRoom($userId, $roomId, $identity)
    {
        if ($identity == 'player') {
            return $this->joinRoomAsPlayer($userId, $roomId);
        }
        return $this->joinRoomAsVisiter($userId, $roomId);
    }

    public function visiterToPlayer($roomId, $userId)
    {
        /** @var Room $room */
        $room = $this->rooms[$roomId];
        $room->setPlayer($userId);
        $room->removeVisiter($userId);
    }

    public function unsertRoom($roomId)
    {
        unset($this->rooms[$roomId]);
    }

    public function createRoomId()
    {
        return substr(md5(time()), 0, 8);
    }

    public function getRoomPlayer($roomId)
    {
        $room = $this->getRoomById($roomId);
        return $room->getPlayers();
    }

    public function getRoomVisiters($roomId)
    {
        $room = $this->getRoomById($roomId);
        return $room->getVisiters();
    }

    public function checkRoomExist($roomId)
    {
        return isset($this->rooms[$roomId]);
    }

    public function speak($userId, $roomId)
    {
        $room = $this->getRoomById($roomId);
        $room->playerSpeak($userId);
    }

    public function spearOver($userId, $roomId)
    {
        $room = $this->getRoomById($roomId);
        $room->playerSpeakOver($userId);
    }

    public function changePlace($userId, $roomId, $newPlaceNum)
    {
        $room = $this->getRoomById($roomId);
        $room->playerChangeplace($userId, $newPlaceNum);
    }

    public function incrDay($roomId)
    {
        $room = $this->getRoomById($roomId);
        $room->incrDay();
    }

    public function inNight($roomId)
    {
        $room = $this->getRoomById($roomId);
        $room->inNight();
    }

    public function inSun($roomId)
    {
        $room = $this->getRoomById($roomId);
        $room->inSun();
    }

    protected function joinRoomAsPlayer($userId, $roomId)
    {
        $room = $this->getRoomById($roomId);
        $room->setPlayer($userId);
        return true;
    }

    protected function joinRoomAsVisiter($userId, $roomId)
    {
        $room = $this->getRoomById($roomId);
        $room->setVisiter($userId);
        return true;
    }

    protected function getRoomById($roomId): Room
    {
        if (! isset($this->rooms[$roomId])) {
            throw new \Exception('房间不存在');
        }
        return $this->rooms[$roomId];
    }
}
